/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package serwer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 *
 * @author nicra
 */
public class BaseManager {
    public Base base;
    
    public void load(){
        try{
            FileInputStream fileIn =
                new FileInputStream("base.db");
            ObjectInputStream in = new ObjectInputStream(fileIn);
            base = (Base) in.readObject();
            Serwer.log("Ładuję bazę.");
            in.close();
            fileIn.close();
        }catch(IOException ex){
            Serwer.log("Bazy nie odnaleziono - tworzę nową.");
            init();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
    }
    public void init(){
        base= new Base();
        
    }
    public void test(){
        Serwer.log("Test bazy: "+base);
    }
    public void reset() {
            File f =
                    new File("base.db");
            if(f.exists()) f.delete();
            
            init();
            Serwer.log("Bazy zresetowana.");
    }
    
    public void save(){
        try{
            FileOutputStream fOut =
                new FileOutputStream("base.db");
            ObjectOutputStream out =
                            new ObjectOutputStream(fOut);
            out.writeObject(base);
            Serwer.log("Zapisuję bazę.");
            out.close();
            fOut.close();
        }catch(IOException i){ 
            i.printStackTrace();
        }
    }
}
